﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblItemsDAO
    {
        private DBService _db = null;
        public TblItemsDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable InsUp(TblItems items, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("TblItems_InsUp",

                    new string[] { "@ItemID", "@ItemName", "@IDMenuChild", "@Price", "@Discount", "@PriceTaxCode", "@KhuyenMai", "@Vote", "@Description", "@Type", "@Mode" },
                    new object[] { items.ItemID, items.ItemName, items.IDMenuChild, items.Price, items.Discount, items.PriceTaxCode, items.KhuyenMai, items.Vote, items.Description, items.Type, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetItems_All()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetListItems(int ItemID, string p_ItemName, string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_Lst",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort" },
                    new object[] { ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public bool UpdatePrice(int ItemID, double Price)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update TblItems set Price = @Price where ItemID = @ItemID",
                    new string[] { "@ItemID", "@Price" },
                    new object[] { ItemID, Price },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // delete san pham
        public bool DeleteItem(int ItemID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete from TblItems where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // Lấy sản phẩm theo ID
        public TblItems GetItemByID(int ItemID)
        {
            TblItems item = new TblItems();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select vItems.* from vItems where vItems.ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ItemID = ItemID;

                    item.ItemName = dr["ItemName"].ToString();
                    item.Description = dr["Description"].ToString();
                    item.Discount = Convert.ToInt32(dr["Discount"].ToString());
                    item.IDMenuChild = Convert.ToInt32(dr["IDMenuChild"].ToString());
                    item.ImageURL = dr["ImageURL"].ToString();
                    item.KhuyenMai = dr["KhuyenMai"].ToString();
                    item.Price = Convert.ToDecimal(dr["Price"].ToString());
                    item.PriceTaxCode = Convert.ToDecimal(dr["PriceTaxCode"].ToString());
                    item.Type = Convert.ToInt32(dr["Type"].ToString());
                    item.Vote = Convert.ToInt32(dr["Vote"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }

        // get product new
        public DataTable GetAllProduct_New(int Type, int IDMenuChild, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetItems_MenuChild", 
                    new string[]{ "@Type", "@IDMenuChild", "@Mode"}, new object[]{ Type, IDMenuChild, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        //update Vote

        public DataTable Update_Vote(int Vote, int ItemID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("update TblItems set Vote = @Vote where ItemID = @ItemID ", 
                    new string[]{"@Vote", "@ItemID" }, new object[]{Vote, ItemID}, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetItem_New()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top(10) * from  vItems order by ItemID DESC", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        //sắp xếp theo gia
        public DataTable SapXepSP(int TangGiam, int GiaVote)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblSortItems",
                    new string[] { "@TangGiam", "@GiaVote" }, new object[] { TangGiam, GiaVote }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // load item by menu parrent
        public DataTable GetAllProduct_ByMenuParrent(int IDMenuParent)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems where IDMenuParent = @IDMenuParent ",
                    new string[] { "@IDMenuParent" }, new object[] { IDMenuParent }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

    }
}
